package com.jh.paymentchannel.util.huizhi;

import cn.jh.common.utils.CommonConstants;
import com.jh.paymentchannel.util.SignUtils;
import net.sf.json.JSONObject;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
public class HuiZhiDeom {
    @Value("${HuiZhi.HZkey}")
    private static String HZkey;

    private static final Logger log = LoggerFactory.getLogger(HuiZhiDeom.class);


    /**
     * 订单查询
     */
    @RequestMapping(method = RequestMethod.POST, value = "v1.0/HuiZhi/order/query")
    public Object orderQuery(@RequestParam(value = "merchantId", required = true) String merchantId,
                             @RequestParam(value = "merOrderId", required = true) String merOrderId,
                             @RequestParam(value = "timeStamp", required = true) String timeStamp
    ) {
        HashMap<Object, Object> map = new HashMap<>();
        //加密验签
        List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
        nvps.add(new BasicNameValuePair("merchantId", merchantId));
        nvps.add(new BasicNameValuePair("merOrderId", merOrderId));
        nvps.add(new BasicNameValuePair("timeStamp", timeStamp));
        try {
            String sign = SignUtils.signMD5Datas(nvps, HZkey);
            /**订单查询*/
            String url = " http://www.allforbenefit.com:8083/HzGateway/kjv3/FastPay/queryOrder.do";
            MultiValueMap<String, String> requestEntity = new LinkedMultiValueMap<String, String>();
            requestEntity.add("merchantId", merchantId);
            requestEntity.add("merOrderId", merOrderId);
            requestEntity.add("timeStamp", timeStamp);
            requestEntity.add("sign", sign);

            RestTemplate restTemplate = new RestTemplate();
            String result = restTemplate.postForObject(url, requestEntity, String.class);
            JSONObject jsonObject = JSONObject.fromObject(result);
            if (jsonObject.isNullObject() || result == null) {
                map.put(CommonConstants.RESP_CODE, CommonConstants.FALIED);
                map.put(CommonConstants.RESP_MESSAGE, "失败");
                log.info("参数获取失败");
                return map;
            } else {
                String resCode = jsonObject.getString("resCode");
                String resMsg = jsonObject.getString("resMsg");
                map.put(CommonConstants.RESP_CODE, CommonConstants.SUCCESS);
                map.put(CommonConstants.RESP_MESSAGE, "订单查询成功");
                map.put(CommonConstants.RESULT, resCode);
                return map;

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return map;

    }

   /* public static void main(String[] args) {
        HashMap<Object, Object> map = new HashMap<>();
        //加密验签
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        String date = df.format(new Date());
        List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
        nvps.add(new BasicNameValuePair("merchantId", "99928989230000"));
        nvps.add(new BasicNameValuePair("merOrderId", "HS20180530155321675"));
        nvps.add(new BasicNameValuePair("timeStamp", date));
        try {
            String sign = SignUtils.signMD5Datas(nvps, "abcd654321");
            *//**订单查询*//*
            String url = "http://www.allforbenefit.com:8083/HzGateway/kjv3/FastPay/queryOrder.do";
            MultiValueMap<String, String> requestEntity = new LinkedMultiValueMap<String, String>();
            requestEntity.add("merchantId", "99928989230000");
            requestEntity.add("merOrderId", "HS20180530155321675");
            requestEntity.add("timeStamp", date);
            requestEntity.add("sign", sign);

            RestTemplate restTemplate = new RestTemplate();
            String result = restTemplate.postForObject(url, requestEntity, String.class);
            JSONObject jsonObject = JSONObject.fromObject(result);
            if (jsonObject.isNullObject() || result == null) {

                log.info("参数获取失败");
            } else {
                String resCode = jsonObject.getString("resCode");
                String resMsg = jsonObject.getString("resMsg");

                System.out.print(result);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }


    }*/
}
